package com.dudsbury.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JTable;

import org.joda.time.DateTime;

import com.dudsbury.jtablestuff.CustomTable;
import com.dudsbury.jtablestuff.JScrollPaneAdjuster;
import com.dudsbury.jtablestuff.JTableRowHeaderResizer;
import com.dudsbury.jtablestuff.RowHeaders;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class MainFrame extends JFrame {

	private JPanel contentPane,panel_1;
	private CustomTable table;
	private JScrollPane pane;
	

	


	/**
	 * Create the frame.
	 */
	public MainFrame(ArrayList<Booking> bookingList,String[] roomList) {
		setTitle("Dudsbury Hotel Event Booking System");
		setJMenuBar(new CustomFileMenu());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1080,640);
		setExtendedState(this.getExtendedState()|JFrame.MAXIMIZED_BOTH);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton btnViewByWeek = new JButton("View By Week");
		btnViewByWeek.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				table.setWeeklyView();
			}
		});
		panel.add(btnViewByWeek);

		JButton btnViewByMonth = new JButton("View By Month");
		btnViewByMonth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				table.setMonthlyView();
			}
		});
		panel.add(btnViewByMonth);

		JButton btnSelectDate = new JButton("Select Date");
		btnSelectDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new CalendarFrame(table);
			}
		});
		panel.add(btnSelectDate);

		panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		
		
		table = new CustomTable(bookingList,roomList);
		RowHeaders rowHeader = new RowHeaders(roomList,table);
		
		 pane = new JScrollPane(table);
		 //pane.setViewportView(table);
		 pane.setRowHeaderView(rowHeader);
		 JTableHeader corner = rowHeader.getTableHeader();
	        corner.setReorderingAllowed(false);
	        corner.setResizingAllowed(false);
	        new JScrollPaneAdjuster(pane);

	        new JTableRowHeaderResizer(pane).setEnabled(true); 
	        
		panel_1.add(pane);
		
		JPanel panel_4 = new JPanel();
		pane.setColumnHeaderView(panel_4);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		contentPane.add(panel_2, BorderLayout.WEST);

		JButton button = new JButton("<<");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				if (table.getCurrentView()==table.WEEKLY_VIEW) {
					DateTime datehold = table.getCurrentDate().getDateTime().minusDays(7);
					table.setCurrentDate(new MyDateTime(datehold));
					table.setWeeklyView();
					System.out.println("sdsdf");
				}
				else {
					DateTime datehold = table.getCurrentDate().getDateTime().minusDays(
							table.getCurrentDate().getDateTime().dayOfMonth().getMaximumValue());
					table.setCurrentDate(new MyDateTime(datehold));
					table.setMonthlyView();
				}
				
			}
		});
		panel_2.add(button);

		JPanel panel_3 = new JPanel();
		contentPane.add(panel_3, BorderLayout.EAST);

		JButton button_1 = new JButton(">>");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (table.getCurrentView()==table.WEEKLY_VIEW) {
					DateTime datehold = table.getCurrentDate().getDateTime().plusDays(7);
					table.setCurrentDate(new MyDateTime(datehold));
					table.setWeeklyView();
					System.out.println("sdsdf");
				}
				else {
					DateTime datehold = table.getCurrentDate().getDateTime().plusDays(
							table.getCurrentDate().getDateTime().dayOfMonth().getMaximumValue());
					table.setCurrentDate(new MyDateTime(datehold));
					table.setMonthlyView();
				}
				
				
				
			}
		});
		panel_3.add(button_1);
		
		JPanel panel_5 = new JPanel();
		contentPane.add(panel_5, BorderLayout.NORTH);
		
		JButton btnCreateMeal = new JButton("Create Meal");
		panel_5.add(btnCreateMeal);
		
		JButton btnNewBooking = new JButton("New Booking");
		panel_5.add(btnNewBooking);
		
		JButton btnViewprintReport = new JButton("View/Print Report");
		panel_5.add(btnViewprintReport);
		
		setVisible(true);
	}

}
